package com.spm.librarianmanagersystem.service.impl;

import com.spm.librarianmanagersystem.mapper.NotificationsMapper;
import com.spm.librarianmanagersystem.pojo.Borrow;
import com.spm.librarianmanagersystem.pojo.Reservation;
import com.spm.librarianmanagersystem.service.NotificationService;
import com.spm.librarianmanagersystem.vo.NotificationsDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  @Class: NotificationServiceImpl
 *  @Author: Wu Yue
 *  @Time: 19:33 2022/4/4
 *  @Description:
 */
@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    NotificationsMapper notificationsMapper;

    @Override
    public List<NotificationsDetails> getNotification(String userId) {

        Date currentTime = new Date(System.currentTimeMillis());
        List<Reservation> reservationNotices = notificationsMapper.getReservationTimeOutCancelNotice(userId, currentTime);
        List<NotificationsDetails> notices = new ArrayList<>();
        for (Reservation reservationNotice : reservationNotices) {
            NotificationsDetails details = new NotificationsDetails();
            details.setType("canceled");
            details.setId(Integer.valueOf(reservationNotice.getReservationId()));
            details.setMsg("你有一本图书超过预约时间，已自动为您取消预约，如您需要，请再次预约！图书ISBN编码为： " + reservationNotice.getISBN());
            notices.add(details);
        }
        List<Borrow> boorowNotices = notificationsMapper.getBorrowTimeOutMessage(userId, currentTime);
        for (Borrow boorowNotice : boorowNotices) {
            NotificationsDetails details = new NotificationsDetails();
            details.setType("overdue");
            details.setId(Integer.valueOf(boorowNotice.getUserId()));
            details.setMsg("你有一本图书超期，请及时归还！图书ISBN编码为： " + boorowNotice.getISBN());
            notices.add(details);
        }
        List<Reservation> reservationCanBorrowNotices = notificationsMapper.getCanBorrowBook(userId, currentTime);
        int count = 0;
        HashMap<String, Integer> ISBN = new HashMap();
        for (Reservation reservationCanBorrowNotice : reservationCanBorrowNotices) {
            if(ISBN.containsKey(reservationCanBorrowNotice.getISBN())) {
                ISBN.put(reservationCanBorrowNotice.getISBN(), ++count);
            } else {
                count = 0;
                ISBN.put(reservationCanBorrowNotice.getISBN(), ++count);
            }
        }
//        System.out.println(ISBN);
        count = 1;
        for (Reservation reservationCanBorrowNotice : reservationCanBorrowNotices) {
            NotificationsDetails details = new NotificationsDetails();
            if(count == 1) {
                details.setType("reservation");
                details.setId(Integer.valueOf(reservationCanBorrowNotice.getReservationId()));
                details.setMsg("你有一本图书可借，请及时借阅，当前库存为：" + ISBN.get(reservationCanBorrowNotice.getISBN()) + "！图书ISBN编码为： " + reservationCanBorrowNotice.getISBN());
                notices.add(details);
            }
            count ++;
            if(count > ISBN.get(reservationCanBorrowNotice.getISBN())){
                count = 1;
            }

        }
        return  notices;
    }
}
